﻿using System;
using System.ComponentModel;
using KKSGameLibrary.Logic;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using System.ServiceModel.Channels;
using KakaSongGame.Contract;
using OnlineGameLibrary.MessageEx;
using OnlineGameLibrary.Contract;

namespace KaKaSongGame.ViewModel
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        Action<Message> _messagechannel;
        Color[] PresetColors = {Colors.Red,Colors.Green,Colors.Yellow,Colors.Black,Colors.Blue,Colors.Brown,Colors.Purple };
        public MainPageViewModel(IList<GamePlayer> players, string playermeid,int puppetcount,TimeSpan actiontimeout,Action<Message> messagechannel)
        {
            MapWindow = new MyRect();

            ActionTimeout = actiontimeout;
            PuppetNumber = puppetcount;

            var gpvms=from gp in players
                        select new PlayerViewModel
                        {
                            GamePlayer = gp,
                            //PlayerActionTimeout = (int)actiontimeout.TotalSeconds,
                        };
            
            PlayerList = new ObservableCollection<PlayerViewModel>(gpvms);
            for (int i = 0; i < PlayerList.Count; i++)
            {
                PlayerList[i].PlayerColor = PresetColors[i];
            }

            PlayerMe = players.First(p => p.ID == playermeid);

            CardMap = new CardMap(players,puppetcount);
            _messagechannel = messagechannel;
        }

        public GameInfo GameInfo { set; get; }

        int puppetnumber;
        public int PuppetNumber
        {
            get
            {
                return puppetnumber;
            }
            set
            {
                if (puppetnumber != value)
                {
                    puppetnumber = value;
                    onPropertyChanged("PuppetNumber");
                }
            }
        }

        TimeSpan actiontimeout;
        public TimeSpan ActionTimeout
        {
            get
            {
                return actiontimeout;
            }
            set
            {
                if (actiontimeout != value)
                {
                    actiontimeout = value;
                    onPropertyChanged("ActionTimeout");
                }
            }
        }

        MyRect mapwindow;
        public MyRect MapWindow
        {
            get
            {
                return mapwindow;
            }
            set
            {
                if (mapwindow != value)
                {
                    mapwindow = value;
                    onPropertyChanged("MapWindow");
                }
            }
        }

        ImageSource minimapimage;
        public ImageSource MiniMapImage
        {
            get
            {
                return minimapimage;
            }
            set
            {
                if (minimapimage != value)
                {
                    minimapimage = value;
                    onPropertyChanged("MiniMapImage");
                }
            }
        }

        CardMap cardmap;
        public CardMap CardMap
        {
            get
            {
                return cardmap;
            }
            set
            {
                if (cardmap != value)
                {
                    cardmap = value;
                    onPropertyChanged("CardMap");
                }
            }
        }

        ObservableCollection<PlayerViewModel> playerlist;
        public ObservableCollection<PlayerViewModel> PlayerList
        {
            get
            {
                return playerlist;
            }
            set
            {
                if (playerlist != value)
                {
                    playerlist = value;
                    onPropertyChanged("PlayerList");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        void onPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyname));
            }
        }

        public GamePlayer PlayerMe { get; set; }

        public IPlayerActionUI PlayerActionUI { set; get; }

        internal void SetCard(CardInMap cardim)
        {
            var msg = KKSGameMessageDefines.RESPONSE_PUT_CARD
                .CreateMessage(cardim);
            
                _messagechannel.Invoke(msg);
        }

        internal void SetOwner(bool isset, int row, int col, OpenDirection dir)
        {
            Message msg=null;
            if (isset)
                msg = KKSGameMessageDefines.RESPONSE_PUT_PUPPET
                    .CreateMessage(new PuppetOperation
                    {
                        IsPutPuppet = true,
                        Column = col,
                        Row = row,
                        Direction = dir
                    });
            else
                msg =
                     KKSGameMessageDefines.RESPONSE_PUT_PUPPET
                .CreateMessage(new PuppetOperation
                {
                    IsPutPuppet = false,
                    Column = col,
                    Row = row,
                    Direction = dir
                });

            _messagechannel.Invoke(msg);
        }
    }
}
